# 从app模块中即从__init__.py中导入创建的app应用
import logging
from flaskapp import *
from services import *
from common import *


# 建立路由，通过路由可以执行其覆盖的方法，可以多个路由指向同一个方法
# 这个其实就相当于在写接口了

@weatherapp.route('/getArea/')
def getArea():
    cip = request.args.get('cip')
    weatherapp.logger.info('/getArea called.cip: ' + cip)

    geolite2 = seperate_ip(cip)
    province_names = trim_district_name(geolite2['Country_SpecificNameCN'])
    city_names = trim_district_name(geolite2['City_NameCN'])
    province = province_names['name']
    city = city_names['name']
    geolite2['Country_SpecificNameCN'] = province
    geolite2['City_NameCN'] = city
    return geolite2


@weatherapp.route('/getWeather/')
def getWeather():
    province = request.args.get('province')
    city = request.args.get('city')
    cip = request.args.get('cip')
    if province != None and city != None:
        weatherapp.logger.info('/getWeather called.province: ' + province + '.city: ' + city)
        weather = service.getWeather(province=province, city=city)
        return weather
    elif cip != None:
        weatherapp.logger.info('/getWeather called.cip: ' + cip)
        weather = service.getWeather(cip=cip)
        return weather


@weatherapp.route('/testWeather')
def testWeather():
    weather = {}
    weather = {
        'city_id': "11304",
        'date': "211013",
        'humidity': "96.0",
        'level': "优",
        'level_num': 0,
        'pm25': 15,
        'precipitation': "0.0",
        'precipitation_type': "无降雨和降雪",
        'publish_time': "2021-10-13 09:31",
        'site': "mlogcn",
        'temperature': 27,
        'temperature_day': 29,
        'temperature_night': 25,
        'update_time': "2021-10-13 09:33",
        'weather': "小雨",
        'weather_day': "小雨",
        'weather_night': "中雨",
        'wind_direction': "东北风",
        'wind_direction_day': "东北风",
        'wind_direction_night': "东北风",
        'wind_direction_num': 1,
        'wind_power': "2级",
        'wind_power_day': "4-5级",
        'wind_power_night': "3-4级",
        'wind_power_num': 2
    }
    return weather

@weatherapp.route('/')
def welcome():
    weatherapp.logger.info('new connection.')
    return 'welcome to come weather-info of my-watch!'
